import java.util.Stack;

class Solution2 {
    public static String removeDuplicates(String s) {
        Stack<Character> st = new Stack<>();
        if(s.length() <= 1){
            return s;
        }
        char[] ch = s.toCharArray();
        st.push(ch[0]);
        for(int i = 1;i<ch.length;i++){
            if(st.isEmpty()){
                st.push(ch[i]);
                continue;
            }
            char tmp = st.peek();
            if(tmp == ch[i]){
                st.pop();
            } else {
                st.push(ch[i]);
            }
        }
        char[] ret = new char[st.size()];
        for(int i = st.size() - 1; i >=0 ;i--){
            ret[i] = st.pop();
        }
        return new String(ret);
    }

    public static void main(String[] args) {
        System.out.println(removeDuplicates("abbaca"));
    }
}